package wineshop.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import wineshop.dao.HotelDao;
import wineshop.domain.Hotel;
import wineshop.exception.DataAccessException;
import wineshop.utils.DBUtils;

public class HotelDaoImpl implements HotelDao {

	private static final String ADD_HOTEL = "insert into hotel(hotel_name,hotel_address,hotel_phone,room_account) values(?,?,?,?)";
	private static final String GET_ALL_HOTEL = "select * from hotel";
	private static final String GET_HOTELBYID = "select * from hotel where hotel_id=?";
    private static final String UPDATE_HOTEL = "update hotel set hotel_name=?,hotel_address=?,hotel_phone=?,room_account=? where hotel_id=?";
	private static final String DEL_HOTEL = "delete from hotel where hotel_id=?";
	
	
	
	public void AddHotel(Hotel hotel) {
		Connection ct = DBUtils.getInstance().getConn();
		PreparedStatement ps = null;

		try {
			ps = ct.prepareStatement(ADD_HOTEL);
			ps.setString(1, hotel.getHotelName());
			ps.setString(2, hotel.getHotelAddress());
			ps.setString(3, hotel.getHotelPhone());
			ps.setInt(4, hotel.getRoomAccount());
            
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DataAccessException("新增分店失败，请检查数据！");
		} finally {

			DBUtils.getInstance().releaseRes(ct, ps, null);
		}

	}

	public List<Hotel> GetAllHotel() {
		Connection ct = DBUtils.getInstance().getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Hotel> hotels = new ArrayList<Hotel>();
		try {
			ps = ct.prepareStatement(GET_ALL_HOTEL);
			rs = ps.executeQuery();
			while (rs.next()) {
				Hotel hotel = new Hotel();
				hotel.setHotelId(rs.getInt(1));
				hotel.setHotelName(rs.getString(2));
				hotel.setHotelAddress(rs.getString(3));
				hotel.setHotelPhone(rs.getString(4));
				hotel.setRoomAccount(rs.getInt(5));

				hotels.add(hotel);

			}

		} catch (SQLException e) {
			e.printStackTrace();
			throw new DataAccessException("酒店列表获取失败!");
		} finally {

			DBUtils.getInstance().releaseRes(ct, ps, null);

		}

		return hotels;
	}

	public Hotel GetHotelById(int hotelId) {
		Connection ct = DBUtils.getInstance().getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Hotel hotel = null;
		
		try {
			ps = ct.prepareStatement(GET_HOTELBYID);
			ps.setInt(1, hotelId);
			rs = ps.executeQuery();
			if (rs.next()) {
				hotel = new Hotel();
				hotel.setHotelId(rs.getInt(1));
				hotel.setHotelName(rs.getString(2));
				hotel.setHotelAddress(rs.getString(3));
				hotel.setHotelPhone(rs.getString(4));
				hotel.setRoomAccount(rs.getInt(5));

			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			DBUtils.getInstance().releaseRes(ct, ps, null);

		}

		return hotel;
	}
    
	public void UpdateHotel(Hotel hotel){
		Connection ct = DBUtils.getInstance().getConn();
		PreparedStatement ps = null;

		try {
			ps = ct.prepareStatement(UPDATE_HOTEL);
			ps.setString(1, hotel.getHotelName());
			ps.setString(2, hotel.getHotelAddress());
			ps.setString(3, hotel.getHotelPhone());
			ps.setInt(4, hotel.getRoomAccount());
			ps.setInt(5, hotel.getHotelId());

			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DataAccessException("更新分店失败，请检查数据！");
		} finally {

			DBUtils.getInstance().releaseRes(ct, ps, null);
		}
		
		
	}

    public void DelHotel(int hotelId){
    	Connection ct = DBUtils.getInstance().getConn();
		PreparedStatement ps = null;

		try {
			ps = ct.prepareStatement(DEL_HOTEL);
			ps.setInt(1, hotelId);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DataAccessException("删除分店失败，请检查数据！");
		} finally {

			DBUtils.getInstance().releaseRes(ct, ps, null);
		}
		
    	
    	
    	
    }
}
